package com.example.mall.controller;

import com.example.mall.dto.request.FilterRequest;
import com.example.mall.dto.response.BaseResponse;
import com.example.mall.model.CommodityInfo;
import com.example.mall.model.Tag;
import com.example.mall.service.CommoityInfoService;
import com.example.mall.service.SellService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@ResponseBody
@RequestMapping("commodity")
public class CommodityController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private CommoityInfoService commoityInfoService;

    @PostMapping("commoditylist")
    public BaseResponse getCommodityList(@RequestParam("sID")Integer sID){
        List<CommodityInfo> list = commoityInfoService.getCommodityList(sID);
        return new BaseResponse(200,"commodityList",list);
    }


    public CommodityInfo getCommodityByCID(Integer cID){
        String sql = "select * from commodityinfo where id = ?";
        RowMapper<CommodityInfo> rowMapper = new BeanPropertyRowMapper<>(CommodityInfo.class);
        CommodityInfo commodityInfo = jdbcTemplate.queryForObject(sql,rowMapper,cID);
        return commodityInfo;
    }

    @PostMapping("deletecommodity")
    public BaseResponse deleteCommodity(@RequestParam("sID")Integer sID,@RequestParam("cID")Integer cID){
        String sql = "select sellerID from commodityinfo where id = ?";
        Integer exactSID = jdbcTemplate.queryForObject(sql,Integer.class,cID);
        BaseResponse response;
        if(Objects.equals(exactSID, sID)) {
            commoityInfoService.deleteCommodity(cID);
            response = BaseResponse.success("deleteOk");
        }else {
            response = BaseResponse.error("noRight");
        }
        return response;
    }


    public List<Integer> getCommodityTagId(Integer cID){
        String sql = "select tID from belong where cID = ?";
        List<Integer> list = jdbcTemplate.queryForList(sql,Integer.class,cID);
        return list;
    }

    @PostMapping("gettag")
    public BaseResponse getTag(@RequestParam("cID")Integer cID){
        List<Tag> tagList = getTagList();
        CommodityInfo commodityInfo = getCommodityByCID(cID);
        List<Integer> tagIds = getCommodityTagId(commodityInfo.getId());
        List<Tag> list = new ArrayList<>();
        for(Integer i: tagIds){
            for(Tag tag: tagList){
                if(tag.getId() == i){
                    list.add(tag);
                    continue;
                }
            }
        }
        return new BaseResponse(200,"commodityWithTag",list);
    }

    public List<Tag> getTagList(){
        String sql = "select * from tag";
        RowMapper<Tag> rowMapper = new BeanPropertyRowMapper<>(Tag.class);
        return jdbcTemplate.query(sql,rowMapper);
    }

    @PostMapping("getcommodityinfo")
    public BaseResponse getCommodityInfo(@RequestParam("cID")Integer cID){
        return new BaseResponse(200,"commodityInfo",commoityInfoService.getCommodityInfo(cID));
    }

    @PostMapping("updatecommoditystatus")
    public BaseResponse updateCommodityStatus(@RequestParam("cID")Integer cID,@RequestParam("flag")String flag){
        commoityInfoService.updateCommodityStatus(cID,flag);
        return BaseResponse.success("updateOk");
    }

    @PostMapping("filtercommoditylist")
    public BaseResponse filterCommodityList(@RequestBody FilterRequest request){
        List<CommodityInfo> list = commoityInfoService.filterCommodity(request.getSellerID(),request.getcID(),request.getShelves(),request.getTagName(),request.getName());
        return BaseResponse.success("filterCommodityList",list);
    }


}
